<template>
  <div class="signature">
    <x-header>签名</x-header>
    <div class="canvasBox" ref="canvasHW">
      <canvas @touchstart='touchStart'
              @touchmove='touchMove'
              @touchend='touchEnd'
              @mousedown="mouseDown"
              @mousemove="mouseMove"
              @mouseup="mouseUp"
              ref="canvasF">
      </canvas>

      <div class="btnBox">
        <button @click="overwrite">重签</button>
        <button @click="commit">确认</button>
      </div>

    </div>
  </div>
</template>

<script>
import {
  XHeader
} from 'vux'
export default {
  name: 'signature',
  components: {
    XHeader
  },
  data () {
    return {
      points: [],
      startX: 0,
      startY: 0,
      moveY: 0,
      moveX: 0,
      endY: 0,
      endX: 0,
      w: null,
      h: null,
      imgData: '',
      isDown: false,
      canvasBoard: null,
      canvasContext: null
    }
  },
  mounted () {
    this.canvasBoard = this.$refs.canvasF
    this.canvasBoard.height = window.innerHeight - 70 - 45
    this.canvasBoard.width = this.$refs.canvasHW.offsetWidth - 20
    this.canvasContext = this.canvasBoard.getContext('2d')
  },
  methods: {
    // Computer event -- Mouse down
    mouseDown (ev) {
      ev = ev || event
      ev.preventDefault()

      let obj = {
        x: ev.offsetX,
        y: ev.offsetY
      }

      this.startX = obj.x
      this.startY = obj.y
      this.canvasContext.beginPath()
      this.canvasContext.moveTo(this.startX, this.startY)
      this.canvasContext.lineTo(obj.x, obj.y)
      this.canvasContext.stroke()
      this.canvasContext.closePath()
      this.points.push(obj)
      this.isDown = true
    },

    // Mobile event -- Touch start
    touchStart (ev) {
      ev = ev || event
      ev.preventDefault()
      if (ev.touches.length === 1) {
        let obj = {
          x: ev.targetTouches[0].clientX,
          y: ev.targetTouches[0].clientY - 48
        }

        this.startX = obj.x
        this.startY = obj.y
        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.points.push(obj)
      }
    },

    // Mobile -- Mouse move
    mouseMove (ev) {
      ev = ev || event
      ev.preventDefault()
      if (this.isDown) {
        let obj = {
          x: ev.offsetX,
          y: ev.offsetY
        }

        this.moveY = obj.y
        this.moveX = obj.x
        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.startY = obj.y
        this.startX = obj.x
        this.points.push(obj)
      }
    },

    // Mobile event -- Touch move
    touchMove (ev) {
      ev = ev || event
      ev.preventDefault()
      if (ev.touches.length === 1) {
        let obj = {
          x: ev.targetTouches[0].clientX,
          y: ev.targetTouches[0].clientY - 48
        }

        this.moveY = obj.y
        this.moveX = obj.x
        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.startY = obj.y
        this.startX = obj.x
        this.points.push(obj)
      }
    },

    // Computer event -- Mouse up
    mouseUp (ev) {
      ev = ev || event
      ev.preventDefault()

      let obj = {
        x: ev.offsetX,
        y: ev.offsetY
      }

      this.canvasContext.beginPath()
      this.canvasContext.moveTo(this.startX, this.startY)
      this.canvasContext.lineTo(obj.x, obj.y)
      this.canvasContext.stroke()
      this.canvasContext.closePath()
      this.points.push(obj)
      this.points.push({x: -1, y: -1})
      this.isDown = false
    },

    // Mobile event TouchEnd
    touchEnd (ev) {
      ev = ev || event
      ev.preventDefault()
      if (ev.touches.length === 1) {
        let obj = {
          x: ev.targetTouches[0].clientX,
          y: ev.targetTouches[0].clientY - 48
        }

        this.canvasContext.beginPath()
        this.canvasContext.moveTo(this.startX, this.startY)
        this.canvasContext.lineTo(obj.x, obj.y)
        this.canvasContext.stroke()
        this.canvasContext.closePath()
        this.points.push(obj)
        this.points.push({x: -1, y: -1})
      }
    },

    // Over write
    overwrite () {
      this.canvasContext.clearRect(0, 0, this.$refs.canvasF.width, this.$refs.canvasF.height)
      this.points = []
    },

    // Commit
    commit () {
      this.$store.state.app.signature.data = this.$refs.canvasF.toDataURL('image/png')
      this.$router.back()
    }
  }
}
</script>

<style lang="less">
  .signatureBox{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow: hidden;
    background: #fff;
    z-index: 100;
    display: flex;
    flex-direction: column;
  }

  .visaDetailTop p{
    margin: 0;
    text-align: center;
    color: #000;
    font-size: 1em;
    position: relative;
  }

  .visaDetailTop p span{
    color: #fff;
    font-size: 1.2em;
  }

  .visaDetailTop p:first-of-type{
    float: left;
  }

  .visaDetailTop p:nth-of-type(2){
    float: right;
  }

  .canvasBox{
    padding: 10px 10px;
    box-sizing: border-box;
    flex: 1;
  }

  canvas{
    border-radius: 5px;
    border: 1px solid lightgray;
  }

  .btnBox{
    height: 30px;
    padding: 5px;
    text-align: right;
    line-height: 30px;
  }

  .btnBox button:first-of-type{
    border: 1px solid lightgray;
    background: transparent;
    border-radius: 4px;
    padding: 5px 10px;
    width: 60px;
    outline:none;
  }

  .btnBox button:last-of-type{
    border: 1px solid lightgray;
    background: #0DBFA5;
    color: #fff;
    border-radius: 4px;
    padding: 5px 10px;
    width: 60px;
    outline:none;
  }

  @media only screen and (min-width: 750px){
    .signatureBox{
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      min-height: 100%;
      box-sizing: border-box;
      overflow: visible;
    }
  }
</style>
